/*
 * 
 * Copyright 2011 visual-analysis-of-cancer.
 * This file is part of visual-analysis-of-cancer.
 * 
 * visual-analysis-of-cancer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * visual-analysis-of-cancer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * visual-analysis-of-cancer:
 * An tool to analye the WHO and GLOBOCAN cancer data.
 * http://code.google.com/p/visual-analysis-of-cancer/
 * 
 * 
 * 
 * Authors: Erik Sniegula, Johannes Dahlke
 * 
 * Module:  Programming in Processing for Visual Analytics
 * Group:   20
 * 
 * Created: Aug. 2011
 * Version: 1.0
 *
 */
 






package de.fuberlin.processing.who.cancer.run;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.NoSuchElementException;

import controlP5.ControlFont;
import controlP5.ControlP5;
import processing.core.PApplet;
import de.fuberlin.processing.who.cancer.controlp5ex.CustomTooltip;
import de.fuberlin.processing.who.cancer.data.DatabaseConfig;
import de.fuberlin.processing.who.cancer.gui.AdditinalInfomationSelector;
import de.fuberlin.processing.who.cancer.gui.DetailGraph;
import de.fuberlin.processing.who.cancer.gui.HealthGraph;
import de.fuberlin.processing.who.cancer.gui.ParameterMenu;
import de.fuberlin.processing.who.cancer.gui.Scatterplot;
import de.fuberlin.processing.who.cancer.utils.ApplicationArgumentProcessor;
import de.fuberlin.processing.who.cancer.utils.Fonts;
import de.fuberlin.processing.who.cancer.utils.InvalidArgumentException;
import de.fuberlin.processing.who.cancer.utils.Notification;

/**
 * This Class consists main initializes of all components such as the
 * scatterplot, the health-graph and the detail-graph
 * 
 */
public class CancerSketch extends PApplet {

	private static final long serialVersionUID = -173118057070134908L;

	private static ApplicationArgumentProcessor arguments;
	
	
	public static final int NUM_SKETCH_WIDTH = 1024;
	public static final int NUM_SKETCH_HEIGHT = 700;
	public static final int NUM_SKETCH_PADDING = 15;

	private ControlP5 controlP5;

	private Scatterplot scatterplot;
	private HealthGraph healthGraph;
	// private WorldGraph worldGraph;
	private DetailGraph detailGraph;
	private ParameterMenu parameterMenu;
	private AdditinalInfomationSelector additinalInfomationSelector;
	
	public CancerSketch() {
		super();
	}

	/**
	 * Processing setup method.
	 */
	@Override
	public void setup() {
		frameRate( 200);
		size( NUM_SKETCH_WIDTH, NUM_SKETCH_HEIGHT);

		controlP5 = new ControlP5(this);
		smooth();
		controlP5.setFont( new ControlFont( Fonts.getDejavuSans()));
		initToolTipSystem();
		initScatterplot();
		initHealthGraph();
		initDetailGraph();
		
		// initWorldGraph();
		//initAdditionalInformationSelector();
		initLocationLister();
	}
	
	
	private void initToolTipSystem() {
		controlP5.setTooltip( new CustomTooltip());
		controlP5.getTooltip().enable();
		//controlP5.getTooltip().getLabel().setMultiline( false);
		controlP5.getTooltip().setDelay( 50);
		controlP5.getTooltip().getLabel().toUpperCase( false);
	}

	/**
	 * Creates and initializes the scatterplot. Registers the scatterplot with
	 * controlP5.
	 */
	private void initScatterplot() {
		// create a new instance of the ScatterPlot controller.
		scatterplot = new Scatterplot( controlP5, "ScatterPlot",
				NUM_SKETCH_WIDTH - Scatterplot.NUM_PLOT_WIDTH
						- NUM_SKETCH_PADDING, NUM_SKETCH_HEIGHT
						- Scatterplot.NUM_PLOT_HEIGHT - NUM_SKETCH_PADDING);
		// register the newly created ScatterPlot with controlP5
		controlP5.register(scatterplot);
	}

	/**
	 * Creates and initializes the health-graph. Registers the health-graph with
	 * controlP5.
	 */
	private void initHealthGraph() {
		healthGraph = new HealthGraph(controlP5, "HealthGraph",
				NUM_SKETCH_PADDING, ParameterMenu.NUM_LIST_HEIGHT);
		controlP5.register(healthGraph);
	}

	/**
	 * Creates and initializes the menu. Registers the menu with controlP5.
	 */
	private void initLocationLister() {
		parameterMenu = new ParameterMenu(controlP5, "LocationLister",
				NUM_SKETCH_PADDING, NUM_SKETCH_PADDING);
		controlP5.register(parameterMenu);
	}

	/*
	 * private void initWorldGraph() { worldGraph = new WorldGraph( controlP5,
	 * "WorldGraph", NUM_SKETCH_WIDTH - WorldGraph.NUM_GRAPH_WIDTH -
	 * NUM_SKETCH_PADDING, NUM_SKETCH_PADDING); controlP5.register( worldGraph);
	 * }
	 */

	/**
	 * Creates and initializes the selector for additional information graphs.
	 */
	private void initAdditionalInformationSelector() {
		additinalInfomationSelector = new AdditinalInfomationSelector(controlP5, "DetailGraph",
				2*NUM_SKETCH_PADDING + HealthGraph.NUM_GRAPH_WIDTH, ParameterMenu.NUM_LIST_HEIGHT);
		controlP5.register( additinalInfomationSelector);
	}
	
	/**
	 * Creates and initializes the detail-graph. Registers the detail-graph with
	 * controlP5.
	 */
	private void initDetailGraph() {
		detailGraph = new DetailGraph(controlP5, "DetailGraph",
				2*NUM_SKETCH_PADDING + HealthGraph.NUM_GRAPH_WIDTH, ParameterMenu.NUM_LIST_HEIGHT);
		controlP5.register(detailGraph);
		scatterplot.setCellDataProcessor( detailGraph);
	}

	/**
	 * Processing draw method.
	 */
	@Override
	public void draw() {
		background(255);
	}
	
	
	
	
	
	
	
	
	
	

	 /**
  * Liest die übergebenen Argumente ein. Analysiert sie auf Syntax und 
  * filtert die akzeptierten Parameter heraus
  * 
  * @param args
  * @throws InvalidArgumentException
  */
 private static void analyseArguments( String[] args) throws InvalidArgumentException {
 	arguments = new ApplicationArgumentProcessor( args, 
 			"db-username", "db-passwd");
 }

	 
		
 /**
  * Untersucht die Argumente, welche der ArgumentProcessor eingelesen hat 
  *  
  * @param args
  * @throws NoSuchElementException 
  * @throws MalformedURLException 
  * @throws IllegalArgumentException
  * 
  */
 private static void processArguments() throws InvalidArgumentException, NoSuchElementException, MalformedURLException  {

 	// Kommunikation: mehr Informationen ausspucken
 	if ( arguments.hasArgument( "verbose") && 
 	     arguments.getValueOfArgument( "verbose").equalsIgnoreCase( "true"))
 		Notification.enableInfoPrinting();
 	
   // Kommunikation: Fehlerausgaben unterdrücken
 	if ( arguments.hasArgument( "errors") && 
        arguments.getValueOfArgument( "errors").equalsIgnoreCase( "suppress")) 
 		Notification.disableErrorPrinting();   
 	
   // Kommunikation: Debug-Informationen ausgeben
 	if ( arguments.hasArgument( "debug") && 
       arguments.getValueOfArgument( "debug").equalsIgnoreCase( "true")) 
 		Notification.enableDebugPrinting();
 	
 	
 	if ( arguments.hasArgument( "db-username") && 
 	    !arguments.getValueOfArgument( "db-username").isEmpty()) {
 		DatabaseConfig.overidePreSettings = false;
 		DatabaseConfig.dbUser = arguments.getValueOfArgument( "db-username");
 	}
 		
 	if ( arguments.hasArgument( "db-passwd") && 
 	    !arguments.getValueOfArgument( "db-passwd").isEmpty()) {
 		DatabaseConfig.overidePreSettings = false;
 		DatabaseConfig.dbPassword = arguments.getValueOfArgument( "db-passwd");
 	}
 }
 
	
	
	private static void runApplet() {
		PApplet.main(new String[] { "--bgcolor=#DFDFDF",
		"de.fuberlin.processing.who.cancer.run.CancerSketch" });
	}
	
	public static void main(String args[]) {
		
	
			try {
				analyseArguments( args);
				
				processArguments();
				
				runApplet();
			} catch ( NoSuchElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch ( MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch ( InvalidArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	
	}

}
